package com.stnet.service.enterprice.chinamobile;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;

import com.modules.commons.Pager;
import com.modules.util.DateHelper;
import com.modules.util.MD5Util;
import com.stnet.bases.BaseManagerDaoImpl;
import com.stnet.entity.Directorder;


/**
 * @author Messcat
 * @version 1.1
 *
 */
public class DirectorderManagerDaoImpl extends BaseManagerDaoImpl implements DirectorderManagerDao {
	
	private static final String PARTNER = "11271";// 代理商id
	private static final String KEY = "W0oQ6wtMWGZ1UKW8zOEjHXcf4aLWFNds5H4pKU5DTeTiOOZLvQR6eesQkuyxRoVL";
	private static final String MOBILE_URL = "http://api.99huafei.com/mobile/direct.aspx";
	private static final String NOTIFY_URL = "http://localhost:8080/MCCMS/indexNewsAction!login.htm";
	private static final String CHARSET = "utf-8";
	private String out_trade_id;

	public DirectorderManagerDaoImpl() {
	}

	public void addDirectorder(Directorder directorder) {
		
		directorderDao.save(directorder);
	}

	public void deleteDirectorder(Long id) {
		directorderDao.delete(id);
	}

	public Pager findDirectorder(int pageSize, int pageNo, String statu) {
		Pager pager = directorderDao.getObjectListByClass(pageSize, pageNo, Directorder.class, statu);
		return pager;
	}

	public Directorder getDirectorder(Long id) {
		return directorderDao.get(id);
	}

	public void updateDirectorder(Directorder directorder) {
		directorderDao.update(directorder);
	}
	
	
	public  List findDirectByAgent(Long agent){
		return directorderDao.findDirectByAgent(agent);
	}
	
	public Pager getListByAgent(int pageSize, int pageNo, Long agent){
		return this.directorderDao.getListByAgent(pageSize, pageNo, agent);
	}
	
	/**
	 * 在线充值接口
	 */
	public int onlineDirectorder(String mobile, int value, int type) {
		out_trade_id = new DateHelper().getRandomNum();//代理商交易序号（唯一）
		int result = 0;
		StringBuilder sb = new StringBuilder();
		StringBuilder sb1 = new StringBuilder();
		StringBuilder sb2 = new StringBuilder();
		sb.append("partner=").append(PARTNER)
		.append("&out_trade_id=").append(out_trade_id)
		.append("&mobile=").append(mobile)
		.append("&value=").append(value)
		.append("&type=").append(type)
		.append("&notify_url=").append(NOTIFY_URL);
		sb1.append(sb.toString()).append("").append(KEY);
		String sign = MD5Util.MD5(sb1.toString());
		
		sb2.append(sb.toString())
		.append("&sign=").append(sign)
		.append("&charset=").append(CHARSET);
		String param = sb2.toString();
		
		String sCurrentLine;
		String sTotalString;
		sCurrentLine = "";
		sTotalString = "";
		InputStream l_urlStream;
		try {
			URL url = new URL(MOBILE_URL);
			URLConnection connection = url.openConnection();
			connection.setDoOutput(true);
			OutputStreamWriter out = new OutputStreamWriter(connection
					.getOutputStream(), "utf-8");
			out.write(param); // post的关键所在！
			out.flush();
			out.close();
			
			l_urlStream = connection.getInputStream();
			// 传说中的三层包装阿！
			BufferedReader l_reader = new BufferedReader(new InputStreamReader(
					l_urlStream, "utf-8"));
			while ((sCurrentLine = l_reader.readLine()) != null) {
				sTotalString += sCurrentLine;
			}
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		int i = sTotalString.indexOf("<result>");
		String strResult = sTotalString.substring(i+8,i+9);
		result = Integer.valueOf(strResult);
		System.out.println(result);
		return result;
	}
	
	
	public static void main(String[] args) {
		String s  ="<?xml version='1.0' encoding='utf-8'?><return><result>6</result><sign>FAD59082CB116A004E9FA767CCDF80FD</sign></return>";
		int i = s.indexOf("<result>");
		String strResult = s.substring(i+8,i+9);
		System.out.println(strResult);
	}
}